$(function(){
	var iscoverName = false;
  function vailPhone(){
      var phone = $(".action-account-check").val();
      var flag = false;
      var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(17[0-9]{1})|(15[0-3]{1})|(15[5-9]{1})|(18[0-9]{1}))+\d{8})$/;
      if(phone.length !=11){
        flag = false;
      }else if(myreg.test(phone)){
        flag = true;
      }
      return flag;
    }

	var count = 120;
	var timeout;
	var BtnCount = function() {
		  var $this = $(".action-get-verifycode");
		  var textCont = $this.children("span").children("span").children("i");
		  if (count == 0) {
			   count=120;
			   $this.removeClass('disabled');
			   textCont.html("");
			   clearTimeout(timeout);
			   $('[data-role="code-img"]').attr('src', wsCommon.host + '/api/member/validateCode?time' + Math.random());
		   }else {
			   count--;
			   textCont.html(count);
			   setTimeout(BtnCount, 1000);
		   }
	   };
		 $(".action-account-check").blur(function(){
			 var _this = $(this);
			 var name = $(".action-account-check").val();
			 $.ajax({
				 url: wsCommon.host + '/api/member/exist',
				 dataType: wsCommon.jsonType,
				 data: {
					 loginAccount: encodeURIComponent(name).replace(/%E2%80%AC/, ''),
				 },
				 success: function(res) {
					 if(res.code == 1) {
						 iosOverlay({
							 text: res.reason,
							 duration: 1500
						 });
						 iscoverName = true;
					 } else {
						//  var timeout;
						//   _this.addClass('disabled');
						//   var textCont = _this.children("span").children("span").children("i");
						//   timeout = setTimeout(BtnCount, 1000);
						iscoverName = false;
						  var name = $(".action-account-check").val();
			  	 		if(!vailPhone()) {
			  	 			iosOverlay({
			  	 				text: "请使用手机号注册!",
			  	 				duration: 1500
			  	 			});
			  	 			return;
			  	 		}

			  	 		var tempN = vailPhone();
			  	 	    if(tempN){
			  	 			$(".action-need-verify").show();
			  	 		}else{
			  	 			$(".action-need-verify").hide();
			  	 		}
					 }

				 },
				 error: function() {
					 // console.log('注册显示接口返回失败');
				 }
			 })
	 	})
	var register = {
		codeImg: function() {
			$('[data-role="code-img"]').attr('src', wsCommon.host + '/api/member/validateCode?time' + Math.random());
			$('[data-role="changeImg"]').on('click' , function() {
				$('[data-role="code-img"]').attr('src', wsCommon.host + '/api/member/validateCode?time' + Math.random());
			})
		},
		sendPhoneCode : function() {
			$('[data-role="sendcode"]').on('click', function() {
				var _this = $(this);
				if($(this).hasClass('disabled')) return;
				var name = $('[data-role="name"]').val();
				var code = $('[data-role="imgcode"]').val();
				if(name == '') {
					iosOverlay({
						text: "账号不能为空!",
						duration: 1500
					});
					return;
				}
				if(code == '') {
					iosOverlay({
						text: "图形验证码不能为空!",
						duration: 1500
					});
					return;
				}
				$.ajax({
					url: wsCommon.host + '/api/member/getSmsCode',
					dataType: wsCommon.jsonType,
					data: {
						mobile: name,
						verifyCode:code
					},
					success: function(res) {
						if(res.code == 0) {
							iosOverlay({
								text: res.reason,
								duration: 1500
							});
							return;
						}
						iosOverlay({
							text: res.reason,
							duration: 1500
						});
						var timeout;
						 _this.addClass('disabled');
						 var textCont = _this.children("span").children("span").children("i");
						 timeout = setTimeout(BtnCount, 1000);
					},
					error: function() {
						// console.log('注册显示接口返回失败');
					}
				})
			})
		},
		sendData: function() {
			$('[data-role="register"]').on('click', function() {
				var name = $('[data-role="name"]').val();
				var imgcode = $('[data-role="imgcode"]').val();
				var password = $('[data-role="password"]').val();
				var repassword = $('[data-role="repassword"]').val();
				var sendcodeval = $('[data-role="sendcodeval"]').val();
				if(name == '') {
					iosOverlay({
						text: "账号不能为空!",
						duration: 1500
					});
					return;
				}
				if(!vailPhone()) {
		 			iosOverlay({
		 				text: "请使用手机号注册!",
		 				duration: 1500
		 			});
		 			return;
		 		}
				if(imgcode == '') {
					iosOverlay({
						text: "图形验证码不能为空!",
						duration: 1500
					});
					return;
				}
				if(password == '') {
					iosOverlay({
						text: "密码不能为空!",
						duration: 1500
					});
					return;
				}
				if(repassword == '') {
					iosOverlay({
						text: "确认密码不能为空!",
						duration: 1500
					});
					return;
				}
				if(password != repassword) {
					iosOverlay({
						text: "密码与确认密码不相符，请重新填写!",
						duration: 1500
					});
					return;
				}
				if(sendcodeval == '' && !iscoverName) {
					iosOverlay({
						text: "短信验证码不能为空!",
						duration: 1500
					});
					return;
				}
				if(!$('#license').prop('checked')) {
					iosOverlay({
						text:"请先同意注册协议!",
						duration: 1500
					});
					return;
				}
				$.ajax({
					url: wsCommon.host + '/api/member/register',
					dataType: wsCommon.jsonType,
					data: {
						loginAccount: name,
						loginPassword: password,
						license: 'on',
						verifyCode: imgcode,
						smsCode: sendcodeval
					},
					success: function(res) {
						if(res.reason == '注册成功') {
							iosOverlay({
								text: "注册成功，正在跳转页面!",
								duration: 1500,
								onbeforehide: function() {
									wsCommon.setCookie('uid', res.data.user.memberId);
									window.location.href = './passport-signup-result.html';
								}
							});
						} else {
							$('[data-role="code-img"]').attr('src', wsCommon.host + '/api/member/validateCode?time' + Math.random());
							$('[data-role="imgcode"]').val('');
							iosOverlay({
								text: res.reason,
								duration: 1500
							});
						}
					},
					error: function() {
						// console.log('注册显示接口返回失败');
					}
				})

			})
		},
		passwordJy: function() {
			//密码强度
			$('#dom_el_64073e1').keyup(function () {
				var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$", "g");
				var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
				var enoughRegex = new RegExp("(?=.{6,}).*", "g");

				if (false == enoughRegex.test($(this).val())) {
					$(".password-check").attr("class","password-check");
					 //密码小于六位的时候，密码强度图片都为灰色
				}
				else if (strongRegex.test($(this).val())) {
					$(".password-check").attr("class","password-check password-strong");
					 //密码为八位及以上并且字母数字特殊字符三项都包括,强度最强
				}
				else if (mediumRegex.test($(this).val())) {
					$(".password-check").attr("class","password-check password-good");
					 //密码为七位及以上并且字母、数字、特殊字符三项中有两项，强度是中等
				}
				else {
					$(".password-check").attr("class","password-check password-weak");
					 //如果密码为6为及以下，就算字母、数字、特殊字符三项都包括，强度也是弱的
				}
				return true;
			});
		},
		init: function() {
			this.codeImg();
			this.sendPhoneCode();
			this.sendData();
			this.passwordJy();
		}
	}

	register.init();
})
